<template>
	<view class="">
		<u-row customStyle="background: #eee; height: 80rpx;padding-left:30rpx;">
			<u-col >
				<u--text prefixIcon='/static/icon/shopN.png' size='15' iconStyle="margin-right:10rpx" v-if="shop.shop_name " :text="'当前店铺：'+shop.shop_name  "></u--text>
				<u--text prefixIcon='/static/icon/shopN.png' size='15' iconStyle="margin-right:10rpx" v-else text="当前店铺： 未选择店铺 "></u--text>
			</u-col>
		</u-row>
		<view style="padding: 0 32rpx;">
			<!-- <u-row>
				<u-col>
					<u--text text='当前店铺'></u--text>
				</u-col>
			</u-row> -->
		<!-- 	<u-dropdown :close-on-click-mask="true" ref="uDropdown" activeColor="#2979ff"
						:borderBottom="true">
				<u-dropdown-item     v-model="value1" 
				:title="title1 || '请选择店铺'"
				@change='dropdownChange' :options="options1">
				 
				</u-dropdown-item>
			 
			</u-dropdown> -->
			
			 <u-row customStyle="margin: 32rpx 0;">
				
				<u-col span="8" offset="2">
					<u-button type="success" text="新增配置" @click="addShelf" size="small"></u-button>
				</u-col>
			 </u-row>
			 <u-swipe-action  >
				
			 	<u-swipe-action-item 
			 		:options="swipe"
			 		 class="u-m-t-30"
			 		 :threshold = '30' 
			 		 v-for="(it,i) in shelfList"
			 		 @click='delet(i)' 
			 	>
			 		 <u-row   customStyle="margin: 16rpx 0;"> 
			 		 	<u-col span="8"  >
			 				<u--text :text='it.shelves_name' size='16'  ></u--text>
			 		 	</u-col>
						
			 		 </u-row>
			 		<u-line  customStyle="margin: 16rpx 0 0 0 ;"></u-line>
			 	</u-swipe-action-item>
			  
			 </u-swipe-action>
			<u-empty
					v-if="shelfList.length == 0"
			        mode="data"
			        icon="http://cdn.uviewui.com/uview/empty/car.png"
			>
			</u-empty>
				<u--text color='#7a7a7a' text='提醒' customStyle="margin-top: 32rpx;"></u--text>
				<u--text color='#7a7a7a' text='1.向左侧滑动,可删除或修改配置,只有店主才有移除权限'></u--text>
				<u--text color='#7a7a7a' text='2.规则调整后,新录入商品立即生效'></u--text>
				<u--text color='#7a7a7a' text='3.若需要调整历史商品临期日期.点击【校准商品】'></u--text>
				 
				<u-modal :show="modalShow"  title='提示'
						cancelText='返回' 
						@cancel='cancel' 
						:showCancelButton=true
						confirmText='去解锁'  
						:content='mContent'
						@close='cancel'
						@confirm="confirm" ref="uModal" 
						:asyncClose="true"></u-modal>
						
		
			</view>
	</view>
	
</template>

<script>
	var that
 import mInit from "../../../tools/Init"
	var ERgoodsShelf = require('../../../service/ERgoodsShelf.js'),mERgoodsShelf
	var ERShoopManage = require('../../../service/ERShoopManage.js'),mERShoopManage
	export default {
		 
		data() {
			return {
				swipe:[{
					  text: '移除'
				}],
				title1:'',
				value1:'1',
				nowShop:{},
				shopId:'',
				shop:{},
				shopList:[],
				shelfList:[],
				options1: [],
				mContent:'货架配置仅对会员开放，升级会员后即可解锁此功能！',
				modalShow: false,
				isVip:false,
				
			}
		},
		onLoad() {
			that = this
			mERShoopManage = new ERShoopManage(this.mNetTool)
			mERgoodsShelf = new ERgoodsShelf(this.mNetTool)
			mInit.init(this.mNetTool, res => {
				let userInfo = uni.getStorageSync('userInfo')
				if(userInfo){
					that.userInfo = userInfo
				}
				 
				this.loadData();
			}) 
		
		},
		onShow() {
			let shelfListConfig =  uni.getStorageSync('shelfListConfig')
			// if(shelfListConfig){
			// 	that.shelfList = shelfListConfig
			// }
			this.loadData()
			//  this.shelfList = get_shelfList
			 
			
		},
		mounted() {
			let userInfo = uni.getStorageSync('userInfo')
			if(!userInfo){
				uni.showModal({
					content:'请登录后使用',
					success(r) {
						if(r.confirm){
							if (that.autoStatus) { //支持一键登录
								console.log('进入一键登录')
								that.keyLogin()
							} else {
								uni.navigateTo({
									url: '../login/login'
								})
							}
						}else {
							uni.navigateBack({
								
							})
						}
					}
				})
			}else { 
				
			} 
		},
	 
		methods: {
			 
			loadData(){
				uni.showToast({
					title:'请稍等',
					icon:'loading',
					duration:1000
				})
				let userInfo = uni.getStorageSync('userInfo')
				let viewShopListItem = uni.getStorageSync('viewShopListItem')
				if(viewShopListItem){
					that.shop = viewShopListItem
					mERgoodsShelf.list({
						store_id:viewShopListItem.id
					},res => {
						console.log('获取货架')
						that.shelfList = res.rows
						uni.setStorageSync('shelfList',res.rows)
					})	
				}else{
					let shopList = uni.getStorageSync('shopList')
					if(shopList){
						that.shop = shopList[0]
						 
					}else{
						if(userInfo){
							
						}else{
							uni.navigateBack({
								
							})
						}
					}
				}
				// let ss = uni.getStorageSync('shopList')
				// if(!ss){
				// 	//如果存在店铺列表
				// 	if(userInfo){
				// 		mERShoopManage.list({
				// 			own_user_id:userInfo.id,
				// 		},R => {
				// 			uni.setStorageSync('shopList',R.rows) 
				// 			that.shopList = R.rows
				// 			ss = R.rows
				// 		})	
				// 	}
				// 	let aa = []
				// 	ss.forEach(item => {
				// 		if(item.shop_name){
				// 			var stmp = {
				// 				label:item.shop_name,
				// 				value:item.id
				// 			}
				// 			aa.push(stmp)
				// 		}
						
				// 	})
				// 	that.options1 = aa
				// 	that.title1 = aa[0].label
				// 	that.shopId = aa[0].value
				// 	mERgoodsShelf.list({
				// 		store_id:aa[0].value
				// 	},res => {
				// 		console.log('获取货架')
				// 		that.shelfList = res.rows
				// 		uni.setStorageSync('shelfList',res.rows)
				// 	})			
				// }else{
				// 	let aa = []
				// 	ss.forEach(item => {
				// 		if(item.shop_name){
				// 			var stmp = {
				// 				label:item.shop_name,
				// 				value:item.id
				// 			}
				// 			aa.push(stmp)
				// 		}
						
				// 	})
				// 	that.options1 = aa
				// 	that.title1 = aa[0].label
				// 	that.shopId = aa[0].value
				// 	mERgoodsShelf.list({
				// 		store_id:aa[0].value
				// 	},res => {
				// 		console.log('获取货架')
				// 		that.shelfList = res.rows
				// 		uni.setStorageSync('shelfList',res.rows)
				// 	})
				// }
				// if(viewShopListItem){
				// 	//如果已经选择过店铺 直接获取货架
				// 	// let shelfListConfig = uni.getStorageSync('shelfListConfig')
				// 	// if(shelfListConfig){
				// 	// 	that.shelfList = shelfListConfig
				// 	// }
				// 	// mERgoodsShelf.list({
				// 	// 	store_id:viewShopListItem.id,
				// 	// },res => {
				// 	// 	that.shelfList = res.rows
				// 	// 	uni.setStorageSync('shelfListConfig',res.rows)
				// 	// })
					
				// }else{
				// 	//如果没有选择 
				// 	viewShopListItem = ss[0]
				// 	mERgoodsShelf.list({
				// 		store_id:viewShopListItem.id,
				// 	},res => {
				// 		that.shelfList = res.rows
				// 		uni.setStorageSync('shelfListConfig',res.rows)
				// 	})
				// }
				
				
			
			 
				 
			},
			dropdownChange(e){
				console.log(e)
				 uni.showLoading({
				 	title:'请稍等'
				 })
				 let shopName
				that.options1.forEach( item => {
					if(item.value == e){
						that.title1 = item.label
						that.shopId = item.value 
						console.log(item.label)
					}
				})
				
				 
				mERgoodsShelf.list({
					store_id:that.shop.id
				},res => {
					that.shelfList =res.rows
					console.log(res)
					uni.setStorageSync('shelfListConfig',res.rows)
					uni.hideLoading()
				})
			},
			getGoodsShopIds(){
				let list = that.shopList
				let shopIds = []
				list.forEach(item => {
					shopIds.push(item.id)
				})
				console.log(shopIds)
				// 查找所有店铺的货架
				// this.mNetTool.getAjaxUrlValue('capi/V2/ERgoodsShelf/erGoodsShelves/list',{ 
				// 	store_id:shopIds.toString(), 
				// },res => {
				// 	that.shelfList = res.rows
				// })
			},
			 
			delet(index){
				console.log(index)
				let shelf ={
					id: that.shelfList[index].id
				}
				 let a = that.shelfList
				 a.splice(index,1) 
				 
				 that.shelfList = a
				 uni.showModal({
				 	title: '提示',
				 	content: '确定删除吗，此操作不可逆！', 
				 	success: res => {
						if(res.confirm){
							mERgoodsShelf.del(shelf,res =>{
								if(res.success){
									let a = that.shelfList
									a.splice(index,1)
									that.shelfList = a
									console.log(a.length)
									uni.$u.toast('移除成功')
									uni.setStorageSync('shelfListConfig',a)
								}else {
									uni.$u.toast('移除失败，请联系客服')
								}
							})
						}
					},
				 	fail: () => {},
				 	complete: () => {}
				 });
				
			},
			addShelf(){
					if(!that.shop.id){
						uni.$u.toast('请选择一个店铺')
						return
					}
					uni.navigateTo({
						url:'addGoodsShelf?shopId='+that.shop.id
					})
				 
			},
			confirm(){
				uni.navigateTo({
					url:'../VIP/VIP'
				})
			},
			cancel(){
				uni.navigateBack({
					
				})
			},
		}
	}
</script>

<style>

</style>
